{% extends "layout/_layout_admin.html" %}
{% block style %}
    <style>
        .more-box {
            position: absolute;
            right: 20px;
            top: 61px;
            width: 140px;
            background-color: #fff;
            z-index: 100;
            border: medium none;
            border-radius: 3px;
            box-shadow: 0 0 3px rgba(86, 96, 117, 0.7);
            display: none;
        }

        .more-box-li {
            cursor: pointer;
        }

        .more-box-li:hover {
            color: #262626;
            background-color: #f5f5f5;
        }

        .more-box-li > span {
            display: block;
            padding: 6px 20px;
            clear: both;
            font-weight: 400;
            line-height: 1.42857143;
            color: inherit;
            white-space: nowrap;
            border-radius: 3px;
            margin: 4px;
            text-align: left;
        }
    </style>
{% endblock %}
{% block content %}
    <div id="box" style="margin: 10px 10px;">
        <div class="ibox contentBox border-all" style="overflow: auto;">
            <div class="content-title border-bottom" style="position:relative;">
                <span>角色管理</span>
            </div>
            <div class="ibox-content" style="position:relative;">
                <button class="btn btn-default pull-right my-btn my-btn-more more-btn" id="more">更多</button>
                <!--点击更多弹出框-->
                <div class="more-box" id="more-box">
                    <div class="more-box-li" onclick="addMembers()">
                        <span>添加成员</span>
                    </div>
                    <div class="more-box-li" onclick="viewMembers()">
                        <span>查看成员</span>
                    </div>
                    <div class="more-box-li" id="author">
                        <span>功能授权</span>
                    </div>
                    <div class="more-box-li" onclick="dataDuthorization()">
                        <span>数据授权</span>
                    </div>
                </div>
                <button class="btn btn-default pull-right my-btn my-btn-pencil" onclick="editBtn()">编辑</button>
                <button class="btn btn-default pull-right my-btn my-btn-remove" onclick="delBtn()">删除</button>
                <button class="btn btn-default pull-right my-btn my-btn-add" onclick="addBtn()">添加</button>
                <button class="btn btn-default my-btn my-search-btn my-btn-search">查询</button>
                <table id="table"
                       data-toggle="table"
                       data-toolbar=".toolbar"
                       data-pagination="true"
                       data-maintain-selected="true"
                       data-search="true"
                       data-show-refresh="true"
                       data-search-align="left"
                       data-toolbar-align="right">
                    <thead>
                    <tr>
                        <th data-width="60" data-align="center" data-field="id">编号</th>
                        <th data-width="220" data-align="center" data-field="name">角色名称</th>
                        <th data-width="90" data-align="center" data-field="code">角色编码</th>
                        <th data-width="90" data-align="center" data-field="user_num">用户数</th>
                        <th data-width="60" data-field="status" data-align="center"
                            data-formatter="checkBoxFormatter">状态
                        </th>
                        <th data-field="description">描述</th>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
        <!--添加 编辑 模态框-->
        <div id="addModal" class="modal fade" style="z-index: 4999!important;">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">{{modalTitle}}</h4>
                    </div>
                    <div class="modal-manage">
                        <div class="form-group col-xs-12">
                            <div class="manage-title">
                                <span><span class="glyphicon glyphicon-asterisk mandatory"></span>角色名称：</span>
                            </div>
                            <input type="hidden" v-model="modalData.id">
                            <input type="text" class="form-control form-manage form-role" placeholder="角色名称"
                                   v-model="modalData.name">
                        </div>
                        <div class="form-group col-xs-12">
                            <div class="manage-title">
                                <span><span class="glyphicon glyphicon-asterisk mandatory"></span>角色编号：</span>
                            </div>
                            <input type="text" class="form-control form-manage form-role" placeholder="角色编号"
                                   v-model="modalData.code">
                        </div>

                        <div class="form-group col-xs-12">
                            <div class="manage-title">
                                <span>状态：</span>
                            </div>
                            <label style="margin-right: 5px;cursor: pointer;">
                                <input type="checkbox" v-model="modalData.status">
                                有效
                            </label>
                        </div>
                        <div class="form-group col-xs-12">
                            <div class="manage-title">
                                <span>角色描述：</span>
                            </div>
                            <textarea class="form-control form-manage form-manage2" style="resize: none"
                                      v-model="modalData.description"></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary submit" onclick="submit()">确认</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>

        <div id="delModal" class="modal fade" style="z-index: 5030!important;">
            <div class="modal-dialog" style="width: 400px;">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">提示</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                    </div>
                    <div class="modal-manage">
                        <img src="static/image/delete.png" alt="" style="float: left;margin-left: 40px;">
                        <h3 class="text-center" style="line-height: 80px;">{{modalDelMsg}}</h3>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary submit" onclick="delData()">确认</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
    </div>

{% endblock %}

{% block script %}
    <script>
        var vm = new Vue({
            el: '#box',
            data: {
                modalTitle: '',
                modalDelMsg: '',
                modalData: {
                    id: 0,
                    code: '',
                    name: '',
                    status: true,
                    description: ''
                }
            },
        });

        $('#table').bootstrapTable({
            url: '/core/account/role/getListJson',
            cache: false, // 设置为 false 禁用 AJAX 数据缓存， 默认为true
            striped: true,  //表格显示条纹，默认为false
            pagination: true, // 在表格底部显示分页组件，默认false
            pageList: [15, 20], // 设置页面可以显示的数据条数
            pageSize: 16, // 页面数据条数
            pageNumber: 1, // 首页页码
            sidePagination: 'client', // 设置为服务器端分页
            exportDataType: "all",   //导出文件方式  支持: 'basic', 'all', 'selected'. basic：本页数据，all，获取服务器所有数据，selected,本页选择行数据
            showExport: true,  //是否显示导出按钮
            buttonsAlign: "right",  //按钮位置
        });

        //为列表添加checkBox样式
        function checkBoxFormatter(value, row, index) {
            if (value) {
                return [
                    '<div id="check-box" class="open1"><div id="check" class="open2"></div></div>'
                ].join('');
            } else {
                return [
                    '<div id="check-box" class="close1"><div id="check" class="close2"></div></div>'
                ].join('');
            }
        }

        var $table = $('#table');
        var selectRow;
        //为bootstrapTable添加每一行的事件监听
        $table.on('click-row.bs.table', function (r, row, element) {
            $('.table-line-active').removeClass('table-line-active');
            $(element).addClass('table-line-active');
            //点击以后将当前行的数据赋值给vm.modalData
            {#vm.modalData = copyFun(row);#}
            selectRow = row;
        });

        var getSelectRowField = function (fieldName) {
            if (selectRow != undefined)
                return selectRow[fieldName];
            else
                return null;
        }


        var submit = function () {
            var newModalData = copyFun(vm.modalData);
            newModalData.status ? newModalData.status = 1 : newModalData.status = 0;
            $.ajax({
                url: '/core/account/role/editRole',
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify(newModalData),
                async: true,
                contentType: 'application/json',
                success: function (response) {
                    $("#addModal").modal('hide');
                    $("#table").bootstrapTable('refreshOptions', {pageNumber: 1});
                    if (response.code == 1)
                        toastr.success('提示：' + response.msg)
                    else
                        toastr.error('提示：' + response.msg)
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    switch (XMLHttpRequest.status) {
                        case(500):
                            toastr.info('服务器系统内部错误');
                            break;
                        case(403):
                            toastr.info("无权限执行此操作");
                            break;
                        case(408):
                            toastr.info("请求超时");
                            break;
                        default:
                            toastr.info(XMLHttpRequest.responseText);
                    }
                }
            });
        }

        var delData = function () {
            $.ajax({
                url: '/core/account/role/delRole',
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify({id: getSelectRowField("id")}),
                async: true,
                contentType: 'application/json',
                success: function (response) {
                    $("#delModal").modal('hide');
                    $("#table").bootstrapTable('refreshOptions', {pageNumber: 1});
                    if (response.code == 1)
                        toastr.success('提示：' + response.msg)
                    else
                        toastr.error('提示：' + response.msg)
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    switch (XMLHttpRequest.status) {
                        case(500):
                            toastr.info('服务器系统内部错误');
                            break;
                        case(403):
                            toastr.info("无权限执行此操作");
                            break;
                        case(408):
                            toastr.info("请求超时");
                            break;
                        default:
                            toastr.info(XMLHttpRequest.responseText);
                    }
                }
            });
        }

        var addBtn = function () {
            vm.modalTitle = '添加角色';
            vm.modalData = {id: 0, code: '', name: '', status: true, description: ''};
            $("#addModal").modal('show');
        }

        var editBtn = function () {
            if (selectRow != undefined) {
                vm.modalTitle = '修改角色';
                vm.modalData.id = getSelectRowField("id")
                vm.modalData.code = getSelectRowField("code")
                vm.modalData.name = getSelectRowField("name")
                vm.modalData.status = getSelectRowField("status")
                vm.modalData.description = getSelectRowField("description")
                $("#addModal").modal('show');
            }
        }

        var delBtn = function () {
            if (selectRow != undefined) {
                vm.modalDelMsg = '是否确认删除角色 【' + selectRow['name'] + '】 ?'
                $("#delModal").modal('show');
            }

        };

        $(function () {
            var num = 0;
            $('.tab-content div').eq(num).fadeIn(100);
            // 点击下一步
            $('.nextStep').click(function () {
                if (num >= 2) {
                    return num = 2;
                } else {
                    num++;
                    upDown()
                }
                $('.grant-title li').eq(num - 1).addClass('complete').removeClass('now');
                $('.grant-title li:eq(' + num + ')').addClass('now');
                $('.tab-content div').eq(num).fadeIn(500).siblings().fadeOut(1)
            });
            //点击上一步
            $('.lastStep').click(function () {
                if (num <= 0) {
                    return num = 0;
                } else {
                    num--;
                    upDown()
                }
                $('.grant-title li').eq(num + 1).removeClass('now complete');
                $('.grant-title li:eq(' + num + ')').addClass('now');
                $('.tab-content div').eq(num).fadeIn(500).siblings().fadeOut(1);
                if (num == 1) {

                }
            });

            function upDown() {
                if (num > 0) {
                    $('.lastStep').removeAttr('disabled');
                    if (num == 2) {
                        $('.nextStep').attr('disabled', 'disabled');
                        $('#submit').removeAttr('disabled')
                    } else if (num < 2) {
                        $('.nextStep').removeAttr('disabled');
                        $('#submit').attr('disabled', 'disabled')
                    }
                } else if (num == 0) {
                    $('.lastStep').attr('disabled', 'disabled')
                }
            }

            // 功能授权显示
            $('#author').click(function () {
                if (!vm.tableLineData) {
                    toastr.warning('您没有选中任何数据项,请选中后再操作！');
                    return;
                }
                num = 0;
                $("#authorization").modal('show');
                $('.grant-title li:eq(' + num + ')').addClass('now').siblings().removeClass('now complete');
                $('.lastStep').attr('disabled', 'disabled');
                $('.nextStep').removeAttr('disabled');
                $('#submit').attr('disabled', 'disabled');
                $('.tab-content div').eq(num).fadeIn(100).siblings().fadeOut();
                workFun()
            })
        });

        //更多点击
        $('#more').click(function () {
            $('#more-box').slideToggle();
            $("body").bind("mousedown", moreDown);
        });

        //点击更多外 关闭
        function moreDown(event) {
            if (!(event.target.id == "more" || event.target.id == "more-box")) {
                $('#more-box').slideUp();
                $("body").unbind("mousedown", moreDown);
            }
        }

        $(document).ready(function () {
            $(".contentBox").css({"height": $(document.body).height() - 20});
            $(window).resize(function () {//当浏览器大小变化时
                $(".contentBox").css({"height": $(document.body).height() - 20})
            });
        });

        //弹出框提示配置
        toastr.options = {
            "closeButton": true,
            "debug": false,
            "progressBar": true,
            "preventDuplicates": false,
            "positionClass": "toast-bottom-right",
            "onclick": null,
            "showDuration": "400",
            "hideDuration": "1000",
            "timeOut": "2000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        };
    </script>
{% endblock %}